<!DOCTYPE html>
<html>
<head><meta name="generator" content="Hexo 3.9.0">
  <meta charset="utf-8">
  
  <title>【日月光华】tensorflow2.0(一)--tf.keras | MaxMa</title>
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  
  <meta name="keywords" content="tf.keras">
  
  
  
  
  <meta name="description" content="本系列将整理我在Tensorflow2.0课程的学习笔记。 由于tf 1.0和tf 2.0的版本更新变化很大，所以，我就直接放弃1.0的学习啦，直接转战2.0了。 【课程】：在网易云课堂上的TF2.0的入门与实战 【授课人】：@日月光华   1.API—-tf.keras基础一般的用法分为5个步骤：  声明model为Sequential()模型 1model = tf.keras.Sequen">
<meta name="keywords" content="tf.keras">
<meta property="og:type" content="article">
<meta property="og:title" content="【日月光华】TensorFlow2.0(一)--tf.keras">
<meta property="og:url" content="https://anxiang1836.github.io/2019/08/31/TF2_Tensorflow2_Keras/index.html">
<meta property="og:site_name" content="MaxMa">
<meta property="og:description" content="本系列将整理我在Tensorflow2.0课程的学习笔记。 由于tf 1.0和tf 2.0的版本更新变化很大，所以，我就直接放弃1.0的学习啦，直接转战2.0了。 【课程】：在网易云课堂上的TF2.0的入门与实战 【授课人】：@日月光华   1.API—-tf.keras基础一般的用法分为5个步骤：  声明model为Sequential()模型 1model = tf.keras.Sequen">
<meta property="og:locale" content="zh-CN">
<meta property="og:image" content="https://raw.githubusercontent.com/anxiang1836/FigureBed/master/img/20190831190156.png">
<meta property="og:image" content="https://raw.githubusercontent.com/anxiang1836/FigureBed/master/img/output_6_1.png">
<meta property="og:image" content="https://raw.githubusercontent.com/anxiang1836/FigureBed/master/img/output_8_0.png">
<meta property="og:image" content="https://raw.githubusercontent.com/anxiang1836/FigureBed/master/img/output_42_0.png">
<meta property="og:image" content="https://raw.githubusercontent.com/anxiang1836/FigureBed/master/img/output_47_0.png">
<meta property="og:updated_time" content="2019-08-31T16:44:06.120Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="【日月光华】TensorFlow2.0(一)--tf.keras">
<meta name="twitter:description" content="本系列将整理我在Tensorflow2.0课程的学习笔记。 由于tf 1.0和tf 2.0的版本更新变化很大，所以，我就直接放弃1.0的学习啦，直接转战2.0了。 【课程】：在网易云课堂上的TF2.0的入门与实战 【授课人】：@日月光华   1.API—-tf.keras基础一般的用法分为5个步骤：  声明model为Sequential()模型 1model = tf.keras.Sequen">
<meta name="twitter:image" content="https://raw.githubusercontent.com/anxiang1836/FigureBed/master/img/20190831190156.png">
  
    <link rel="alternate" href="/atom.xml" title="MaxMa" type="application/atom+xml">
  

  

  <link rel="icon" href="/css/images/mylogo.jpg">
  <link rel="apple-touch-icon" href="/css/images/mylogo.jpg">
  
    <link href="//fonts.googleapis.com/css?family=Source+Code+Pro" rel="stylesheet" type="text/css">
  
  <link href="https://fonts.googleapis.com/css?family=Open+Sans|Montserrat:700" rel="stylesheet" type="text/css">
  <link href="https://fonts.googleapis.com/css?family=Roboto:400,300,300italic,400italic" rel="stylesheet" type="text/css">
  <link href="//netdna.bootstrapcdn.com/font-awesome/4.0.3/css/font-awesome.css" rel="stylesheet">
  <style type="text/css">
    @font-face{font-family:futura-pt; src:url("css/fonts/FuturaPTBold.otf") format("woff");font-weight:500;font-style:normal;}
    @font-face{font-family:futura-pt-light; src:url("css/fonts/FuturaPTBook.otf") format("woff");font-weight:lighter;font-style:normal;}
    @font-face{font-family:futura-pt-italic; src:url("css/fonts/FuturaPTBookOblique.otf") format("woff");font-weight:400;font-style:italic;}
}

  </style>
  <link rel="stylesheet" href="/css/style.css">

  <script src="/js/jquery-3.1.1.min.js"></script>
  <script src="/js/bootstrap.js"></script>

  <!-- Bootstrap core CSS -->
  <link rel="stylesheet" href="/css/bootstrap.css">

  
    <link rel="stylesheet" href="/css/dialog.css">
  

  

  
    <link rel="stylesheet" href="/css/header-post.css">
  

  
  
  

</head>
</html>


  <body data-spy="scroll" data-target="#toc" data-offset="50">


  
  <div id="container">
    <div id="wrap">
      
        <header>

    <div id="allheader" class="navbar navbar-default navbar-static-top" role="navigation">
        <div class="navbar-inner">
          
          <div class="container"> 
            <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
              <span class="sr-only">Toggle navigation</span>
              <span class="icon-bar"></span>
              <span class="icon-bar"></span>
              <span class="icon-bar"></span>
            </button>

            
              <a class="brand" style="
                 margin-top: 0px;"  
                href="#" data-toggle="modal" data-target="#myModal" >
                  <img width="124px" height="124px" alt="Hike News" src="/css/images/mylogo.jpg">
              </a>
            
            
            <div class="navbar-collapse collapse">
              <ul class="hnav navbar-nav">
                
                  <li> <a class="main-nav-link" href="/">首页</a> </li>
                
                  <li> <a class="main-nav-link" href="/archives">归档</a> </li>
                
                  <li> <a class="main-nav-link" href="/categories">分类</a> </li>
                
                  <li> <a class="main-nav-link" href="/tags">标签</a> </li>
                
                  <li> <a class="main-nav-link" href="/about">关于</a> </li>
                
                  <li><div id="search-form-wrap">

    <form class="search-form">
        <input type="text" class="ins-search-input search-form-input" placeholder="" />
        <button type="submit" class="search-form-submit"></button>
    </form>
    <div class="ins-search">
    <div class="ins-search-mask"></div>
    <div class="ins-search-container">
        <div class="ins-input-wrapper">
            <input type="text" class="ins-search-input" placeholder="请输入关键词..." />
            <span class="ins-close ins-selectable"><i class="fa fa-times-circle"></i></span>
        </div>
        <div class="ins-section-wrapper">
            <div class="ins-section-container"></div>
        </div>
    </div>
</div>
<script>
(function (window) {
    var INSIGHT_CONFIG = {
        TRANSLATION: {
            POSTS: '文章',
            PAGES: '页面',
            CATEGORIES: '分类',
            TAGS: '标签',
            UNTITLED: '(无标题)',
        },
        ROOT_URL: '/',
        CONTENT_URL: '/content.json',
    };
    window.INSIGHT_CONFIG = INSIGHT_CONFIG;
})(window);
</script>
<script src="/js/insight.js"></script>

</div></li>
            </div>
          </div>
                
      </div>
    </div>

</header>



      
            
      <div id="content" class="outer">
        
          <section id="main" style="float:none;"><article id="post-TF2_Tensorflow2_Keras" style="width: 75%; float:left;" class="article article-type-post" itemscope itemprop="blogPost" >
  <div id="articleInner" class="article-inner">
    
    
      <header class="article-header">
        
  
    <h1 class="thumb" class="article-title" itemprop="name">
      【日月光华】TensorFlow2.0(一)--tf.keras
    </h1>
  

      </header>
    
    <div class="article-meta">
      
	<a href="/2019/08/31/TF2_Tensorflow2_Keras/" class="article-date">
	  <time datetime="2019-08-31T14:39:46.000Z" itemprop="datePublished">2019-08-31</time>
	</a>

      
    <a class="article-category-link" href="/categories/tensorflow/">tensorflow</a>

      
	<a class="article-views">
	<span id="busuanzi_container_page_pv">
		阅读量<span id="busuanzi_value_page_pv"></span>
	</span>
	</a>

      

    </div>
    <div class="article-entry" itemprop="articleBody">
      
        <blockquote>
<p>本系列将整理我在Tensorflow2.0课程的学习笔记。</p>
<p>由于tf 1.0和tf 2.0的版本更新变化很大，所以，我就直接放弃1.0的学习啦，直接转战2.0了。</p>
<p>【课程】：在<a href="https://study.163.com/" target="_blank" rel="noopener">网易云课堂</a>上的<a href="https://study.163.com/course/courseMain.htm?courseId=1004573006&amp;share=1&amp;shareId=1397138252" target="_blank" rel="noopener">TF2.0的入门与实战</a></p>
<p>【授课人】：<a href="https://study.163.com/instructor/1019173582.htm?_trace_c_p_k2_=e6ba91c5609a49d9a4ea1882ea483f74" target="_blank" rel="noopener">@日月光华</a></p>
<p><img src="https://raw.githubusercontent.com/anxiang1836/FigureBed/master/img/20190831190156.png" style="zoom 80%"></p>
</blockquote>
<h1 id="1-API—-tf-keras基础"><a href="#1-API—-tf-keras基础" class="headerlink" title="1.API—-tf.keras基础"></a>1.API—-tf.keras基础</h1><p>一般的用法分为5个步骤：</p>
<ol>
<li><p>声明model为Sequential()模型</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">model = tf.keras.Sequential()</span><br></pre></td></tr></table></figure>
</li>
<li><p>给model从“输入”到“输出”按顺序添加各层（包括层的类型、激活函数、神经元个数等）</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">model.add(tf.keras.layers.Flatten(input_shape=(<span class="number">28</span>,<span class="number">28</span>)))  <span class="comment"># 28*28</span></span><br><span class="line">model.add(tf.keras.layers.Dense(<span class="number">128</span>, activation=<span class="string">'relu'</span>))</span><br><span class="line">model.add(tf.keras.layers.Dense(<span class="number">10</span>, activation=<span class="string">'softmax'</span>))</span><br></pre></td></tr></table></figure>
</li>
<li><p>查看一下所构建的model有没有问题</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">model.summary()</span><br></pre></td></tr></table></figure>
</li>
<li><p>编译模型：指定优化器、LossFunction、metrics</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">model.compile(optimizer=<span class="string">'adam'</span>,</span><br><span class="line">              <span class="comment"># 如果是序列的类别例如：1,2,..,9，用sparse_categorical_crossentropy</span></span><br><span class="line">              <span class="comment"># 如果是OneHot类别，用categorical_crossentropy</span></span><br><span class="line">              loss=<span class="string">'sparse_categorical_crossentropy'</span>,</span><br><span class="line">              metrics=[<span class="string">'acc'</span>])</span><br></pre></td></tr></table></figure>
</li>
<li><p>fit训练样本</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">history = model.fit(X,y,epochs = <span class="number">10</span>)</span><br></pre></td></tr></table></figure>
</li>
<li><p>evaluate测试样本</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">loss,accuracy = model.evaluate(test_X,text_y)</span><br></pre></td></tr></table></figure>
</li>
</ol>
<p>下面在fashion_minist数据集上进行一个应用。</p>
<h1 id="2-fashion-mnist分类练习"><a href="#2-fashion-mnist分类练习" class="headerlink" title="2.fashion_mnist分类练习"></a>2.fashion_mnist分类练习</h1><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> tensorflow <span class="keyword">as</span> tf</span><br><span class="line"><span class="keyword">import</span> pandas <span class="keyword">as</span> pd</span><br><span class="line"><span class="keyword">import</span> numpy <span class="keyword">as</span> np</span><br><span class="line"><span class="keyword">import</span> matplotlib.pyplot <span class="keyword">as</span> plt</span><br><span class="line"><span class="keyword">import</span> seaborn <span class="keyword">as</span> sns</span><br><span class="line">%matplotlib inline</span><br></pre></td></tr></table></figure>
<h2 id="2-1数据读取"><a href="#2-1数据读取" class="headerlink" title="2.1数据读取"></a>2.1数据读取</h2><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">(train_image, train_label), (test_image, test_label) = tf.keras.datasets.fashion_mnist.load_data()</span><br></pre></td></tr></table></figure>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">train_image.shape,train_label.shape</span><br></pre></td></tr></table></figure>
<p>【输出】：((60000, 28, 28), (60000,))</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">test_image.shape, test_label.shape</span><br></pre></td></tr></table></figure>
<p>【输出】：((10000, 28, 28), (10000,))</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">plt.rcParams[<span class="string">'figure.figsize'</span>]=(<span class="number">45</span>,<span class="number">3</span>)</span><br><span class="line">plt.imshow(train_image[<span class="number">0</span>])</span><br></pre></td></tr></table></figure>
<p><img src="https://raw.githubusercontent.com/anxiang1836/FigureBed/master/img/output_6_1.png"></p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 将label进行转为pd，进行数据操作</span></span><br><span class="line">index_of_trainLabel = pd.DataFrame(train_label)</span><br><span class="line">index_of_trainLabel.columns = [<span class="string">'label'</span>]</span><br></pre></td></tr></table></figure>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 简单查看一下整个数据集中的10张</span></span><br><span class="line">plt.rcParams[<span class="string">'figure.figsize'</span>]=(<span class="number">12</span>,<span class="number">10</span>)</span><br><span class="line"><span class="comment"># category = 类别号</span></span><br><span class="line"><span class="keyword">for</span> category <span class="keyword">in</span> range(<span class="number">10</span>):</span><br><span class="line">    <span class="comment"># 在category类别下，有哪些数据</span></span><br><span class="line">    label_list = index_of_trainLabel[index_of_trainLabel[<span class="string">'label'</span>]== category].index.to_list()</span><br><span class="line">    <span class="keyword">for</span> l <span class="keyword">in</span> range(<span class="number">1</span>,<span class="number">11</span>):</span><br><span class="line">        plt.subplot(<span class="number">10</span>,<span class="number">10</span>,(category)*<span class="number">10</span> + l)</span><br><span class="line">        <span class="comment"># 选择在label_list中的前10个输出</span></span><br><span class="line">        plt.imshow(train_image[label_list[l]])</span><br><span class="line">plt.show()</span><br></pre></td></tr></table></figure>
<p><img src="https://raw.githubusercontent.com/anxiang1836/FigureBed/master/img/output_8_0.png" style="zoom 70%"></p>
<h2 id="2-2-数据归一化"><a href="#2-2-数据归一化" class="headerlink" title="2.2 数据归一化"></a>2.2 数据归一化</h2><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 数据的归一化（因为是从0-255，所以直接按照比例缩放就完了）</span></span><br><span class="line">train_image = train_image/<span class="number">255</span></span><br><span class="line">test_image = test_image/<span class="number">255</span></span><br></pre></td></tr></table></figure>
<h2 id="2-3-按label类型分情况训练"><a href="#2-3-按label类型分情况训练" class="headerlink" title="2.3 按label类型分情况训练"></a>2.3 按label类型分情况训练</h2><p>这里面Label的类型有2中类型：</p>
<ul>
<li><p>像label为{1,2,3,4,5,6}这种是为序列编码：</p>
<ul>
<li>LossFunction为：sparse_categorical_crossentropy</li>
</ul>
</li>
<li><p>像label为{0,0,0,1,0,0}这种是为One-Hot编码：</p>
<ul>
<li><p>调用函数，可将序列编码转换为One-Hot编码：</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">tf.keras.utils.to_categorical(train_label)</span><br></pre></td></tr></table></figure>
</li>
<li><p>LossFunction为：categorical_crossentropy</p>
</li>
</ul>
</li>
</ul>
<h3 id="2-3-1-label为序列编码"><a href="#2-3-1-label为序列编码" class="headerlink" title="2.3.1 label为序列编码"></a>2.3.1 label为序列编码</h3><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">model = tf.keras.Sequential()</span><br><span class="line"><span class="comment"># 直接把图片展成为28*28的向量</span></span><br><span class="line">model.add(tf.keras.layers.Flatten(input_shape=(<span class="number">28</span>,<span class="number">28</span>)))  <span class="comment"># 28*28</span></span><br><span class="line">model.add(tf.keras.layers.Dense(<span class="number">128</span>, activation=<span class="string">'relu'</span>))</span><br><span class="line">model.add(tf.keras.layers.Dense(<span class="number">10</span>, activation=<span class="string">'softmax'</span>))</span><br></pre></td></tr></table></figure>
<p>按照上述的Sequential定义的模型，结构可表示为如下：</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line">Model: "sequential"</span><br><span class="line">_________________________________________________________________</span><br><span class="line">Layer (type)                 Output Shape              Param <span class="comment">#   </span></span><br><span class="line">=================================================================</span><br><span class="line">flatten (Flatten)            (None, 784)               0         </span><br><span class="line">_________________________________________________________________</span><br><span class="line">dense (Dense)                (None, 128)               100480    </span><br><span class="line">_________________________________________________________________</span><br><span class="line">dense_1 (Dense)              (None, 10)                1290      </span><br><span class="line">=================================================================</span><br><span class="line">Total params: 101,770</span><br><span class="line">Trainable params: 101,770</span><br><span class="line">Non-trainable params: 0</span><br><span class="line">_________________________________________________________________</span><br></pre></td></tr></table></figure>
<p>下面对模型进行编译（compile），并进行训练（fit）：</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">model.compile(optimizer=<span class="string">'adam'</span>,</span><br><span class="line">              loss=<span class="string">'sparse_categorical_crossentropy'</span>,</span><br><span class="line">              metrics=[<span class="string">'acc'</span>]</span><br><span class="line">)</span><br><span class="line">model.fit(train_image, train_label, epochs=<span class="number">5</span>)</span><br></pre></td></tr></table></figure>
<p>调用evaluate函数，可以输出在测试集上的loss和acc：</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">loss,accuracy = model.evaluate(test_image, test_label)</span><br><span class="line">print(<span class="string">"在测试集上的Loss为：&#123;&#125;，Acc为：&#123;&#125;"</span>.format(loss,accuracy))</span><br></pre></td></tr></table></figure>
<p>【输出】：在测试集上的Loss为：0.3420594166994095，Acc为：0.8761000037193298</p>
<h3 id="2-3-2-label为One-Hot编码"><a href="#2-3-2-label为One-Hot编码" class="headerlink" title="2.3.2 label为One-Hot编码"></a>2.3.2 label为One-Hot编码</h3><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 将顺序编码转换为One-Hot编码</span></span><br><span class="line">train_label_onehot = tf.keras.utils.to_categorical(train_label)</span><br><span class="line">test_label_onehot = tf.keras.utils.to_categorical(test_label)</span><br><span class="line"></span><br><span class="line"><span class="comment"># 构建Sequential模型</span></span><br><span class="line">model = tf.keras.Sequential()</span><br><span class="line">model.add(tf.keras.layers.Flatten(input_shape=(<span class="number">28</span>,<span class="number">28</span>)))  <span class="comment"># 28*28</span></span><br><span class="line">model.add(tf.keras.layers.Dense(<span class="number">128</span>, activation=<span class="string">'relu'</span>))</span><br><span class="line">model.add(tf.keras.layers.Dense(<span class="number">10</span>, activation=<span class="string">'softmax'</span>))</span><br></pre></td></tr></table></figure>
<p>这里面，对于优化器，可以修改其中的超参数lerning_rate等。</p>
<p>所有的优化器都在tf.keras.optimizers中。</p>
<p>【Tips】：在一个方法调用时，按shift+tab可以查看API的描述</p>
<p>下面对模型进行编译（compile），并进行训练（fit）：</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">model.compile(optimizer=tf.keras.optimizers.Adam(learning_rate=<span class="number">0.01</span>),</span><br><span class="line">              loss=<span class="string">'categorical_crossentropy'</span>,</span><br><span class="line">              metrics=[<span class="string">'acc'</span>]</span><br><span class="line">)</span><br><span class="line">model.fit(train_image, train_label_onehot, epochs=<span class="number">5</span>)</span><br></pre></td></tr></table></figure>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 用predict来进行预测</span></span><br><span class="line">predict = model.predict(test_image)</span><br></pre></td></tr></table></figure>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">predict[<span class="number">0</span>]</span><br></pre></td></tr></table></figure>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">array([1.9299150e-13, 5.0559784e-23, 9.1170503e-19, 4.4142470e-22,</span><br><span class="line">       9.1442269e-17, 8.5615501e-02, 1.4840338e-16, 5.4630977e-03,</span><br><span class="line">       8.3949581e-10, 9.0892136e-01], dtype=float32)</span><br></pre></td></tr></table></figure>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">print(<span class="string">"预测的值为：&#123;&#125;，测试数据的值为：&#123;&#125;"</span>.format(np.argmax(predict[<span class="number">0</span>]),test_label[<span class="number">0</span>]))</span><br></pre></td></tr></table></figure>
<p>【输出】：预测的值为：9，测试数据的值为：9</p>
<h3 id="Q1：evaluate和predict区别"><a href="#Q1：evaluate和predict区别" class="headerlink" title="Q1：evaluate和predict区别?"></a>Q1：evaluate和predict区别?</h3><p>在上面的2个例子中，分别用到了evaluate和predict。下面来分别解释一下两个API的作用：</p>
<ul>
<li><p>model.evaluate:</p>
<p>  输入数据和标签,输出损失和精确度.</p>
</li>
<li><p>model.predict:</p>
<p>  输入测试数据,输出预测结果,(通常用在需要将预测结果与真实标签相比较的时候)</p>
</li>
</ul>
<h3 id="Q2：网络容量"><a href="#Q2：网络容量" class="headerlink" title="Q2：网络容量"></a>Q2：网络容量</h3><ul>
<li>网络中的神经元越多，层数越多，神经网络的网络容量越大，拟合能力越强。</li>
<li>但是网络容量越大，训练速度、难度越大，越容易产生过拟合。</li>
</ul>
<h3 id="Q3：提高网络的拟合能力"><a href="#Q3：提高网络的拟合能力" class="headerlink" title="Q3：提高网络的拟合能力"></a>Q3：提高网络的拟合能力</h3><ul>
<li>单纯的增加神经元的个数对于网络的性能提升并不明显</li>
<li>增加层会大大提高网络的拟合能力</li>
</ul>
<p>因此，现在深度学习的深度会越来越深；但是单层的神经元的个数也不能太小，会造成信息瓶颈，容纳不了这一层的信息，使得模型欠拟合</p>
<h3 id="Q4：超参数选择的原则"><a href="#Q4：超参数选择的原则" class="headerlink" title="Q4：超参数选择的原则"></a>Q4：超参数选择的原则</h3><ol>
<li>首先开发一个过拟合的模型：<ul>
<li>添加更多的层</li>
<li>让每一层神经元更多</li>
<li>训练更多的轮次</li>
</ul>
</li>
<li>然后，抑制过拟合：（在没有更多的数据的情况下）<ul>
<li>dropout</li>
<li>正则化（L2）</li>
<li>图像增强</li>
</ul>
</li>
<li>再调节其他的超参数，交叉验证：<ul>
<li>学习速率</li>
<li>隐藏层单元数</li>
<li>训练轮次</li>
</ul>
</li>
</ol>
<h2 id="2-4-Dropout防止过拟合"><a href="#2-4-Dropout防止过拟合" class="headerlink" title="2.4 Dropout防止过拟合"></a>2.4 Dropout防止过拟合</h2><p><strong>Q：为什么Dropout可以解决过拟合？</strong></p>
<p>​    有点类似于随机森林的作用机制，通过多个epoch的迭代在不同的神经元上失活，最后在测试和预测的时候，全部神经元激活，就很像随机森林的多棵树训练然后最后取平均的样子。</p>
<p>下面的例子增加层的个数，同时为了避免过拟合，在每层之后，都增加Dropout，使得神经元随机失活。</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">model = tf.keras.Sequential()</span><br><span class="line">model.add(tf.keras.layers.Flatten(input_shape=(<span class="number">28</span>,<span class="number">28</span>)))  <span class="comment"># 28*28</span></span><br><span class="line">model.add(tf.keras.layers.Dense(<span class="number">128</span>, activation=<span class="string">'relu'</span>))</span><br><span class="line">model.add(tf.keras.layers.Dropout(<span class="number">0.5</span>))</span><br><span class="line">model.add(tf.keras.layers.Dense(<span class="number">128</span>, activation=<span class="string">'relu'</span>))</span><br><span class="line">model.add(tf.keras.layers.Dropout(<span class="number">0.5</span>))</span><br><span class="line">model.add(tf.keras.layers.Dense(<span class="number">128</span>, activation=<span class="string">'relu'</span>))</span><br><span class="line">model.add(tf.keras.layers.Dropout(<span class="number">0.5</span>))</span><br><span class="line">model.add(tf.keras.layers.Dense(<span class="number">10</span>, activation=<span class="string">'softmax'</span>))</span><br></pre></td></tr></table></figure>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">model.summary()</span><br></pre></td></tr></table></figure>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line">Model: "sequential_2"</span><br><span class="line">_________________________________________________________________</span><br><span class="line">Layer (type)                 Output Shape              Param <span class="comment">#   </span></span><br><span class="line">=================================================================</span><br><span class="line">flatten_2 (Flatten)          (None, 784)               0         </span><br><span class="line">_________________________________________________________________</span><br><span class="line">dense_4 (Dense)              (None, 128)               100480    </span><br><span class="line">_________________________________________________________________</span><br><span class="line">dropout (Dropout)            (None, 128)               0         </span><br><span class="line">_________________________________________________________________</span><br><span class="line">dense_5 (Dense)              (None, 128)               16512     </span><br><span class="line">_________________________________________________________________</span><br><span class="line">dropout_1 (Dropout)          (None, 128)               0         </span><br><span class="line">_________________________________________________________________</span><br><span class="line">dense_6 (Dense)              (None, 128)               16512     </span><br><span class="line">_________________________________________________________________</span><br><span class="line">dropout_2 (Dropout)          (None, 128)               0         </span><br><span class="line">_________________________________________________________________</span><br><span class="line">dense_7 (Dense)              (None, 10)                1290      </span><br><span class="line">=================================================================</span><br><span class="line">Total params: 134,794</span><br><span class="line">Trainable params: 134,794</span><br><span class="line">Non-trainable params: 0</span><br><span class="line">_________________________________________________________________</span><br></pre></td></tr></table></figure>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">model.compile(optimizer=tf.keras.optimizers.Adam(learning_rate=<span class="number">0.001</span>),</span><br><span class="line">              loss=<span class="string">'categorical_crossentropy'</span>,</span><br><span class="line">              metrics=[<span class="string">'acc'</span>]</span><br><span class="line">)</span><br></pre></td></tr></table></figure>
<h3 id="fit中添加validation-data"><a href="#fit中添加validation-data" class="headerlink" title="fit中添加validation_data"></a>fit中添加validation_data</h3><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">history = model.fit(train_image, train_label_onehot, </span><br><span class="line">                    epochs=<span class="number">10</span>, </span><br><span class="line">                    validation_data=(test_image, test_label_onehot))</span><br></pre></td></tr></table></figure>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">history.history.keys()</span><br></pre></td></tr></table></figure>
<p>【输出】：dict_keys([‘loss’, ‘acc’, ‘val_loss’, ‘val_acc’])</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line">plt.rcParams[<span class="string">'figure.figsize'</span>]=(<span class="number">10</span>,<span class="number">4</span>)</span><br><span class="line"></span><br><span class="line"><span class="comment"># ACC的子图</span></span><br><span class="line">plt.subplot(<span class="number">1</span>,<span class="number">2</span>,<span class="number">1</span>)</span><br><span class="line">plt.plot(history.epoch,history.history.get(<span class="string">'acc'</span>),label=<span class="string">'train_acc'</span>)</span><br><span class="line">plt.plot(history.epoch,history.history.get(<span class="string">'val_acc'</span>),label=<span class="string">'val_acc'</span>)</span><br><span class="line">plt.legend()</span><br><span class="line"></span><br><span class="line"><span class="comment"># Loss的子图</span></span><br><span class="line">plt.subplot(<span class="number">1</span>,<span class="number">2</span>,<span class="number">2</span>)</span><br><span class="line">plt.plot(history.epoch,history.history.get(<span class="string">'loss'</span>),label=<span class="string">'train_loss'</span>)</span><br><span class="line">plt.plot(history.epoch,history.history.get(<span class="string">'val_loss'</span>),label=<span class="string">'val_loss'</span>)</span><br><span class="line">plt.legend()</span><br><span class="line"></span><br><span class="line">plt.show()</span><br></pre></td></tr></table></figure>
<p><img src="https://raw.githubusercontent.com/anxiang1836/FigureBed/master/img/output_42_0.png" style="zoom 85%"></p>
<h3 id="Q5：过拟合-amp-amp-欠拟合"><a href="#Q5：过拟合-amp-amp-欠拟合" class="headerlink" title="Q5：过拟合&amp;&amp;欠拟合"></a>Q5：过拟合&amp;&amp;欠拟合</h3><ul>
<li>过拟合： 在训练数据上得分很高， 在测试数据上得分相对比较低</li>
<li>欠拟合：  在训练数据上得分比较低， 在测试数据上得分相对比较低</li>
</ul>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">model = tf.keras.Sequential()</span><br><span class="line">model.add(tf.keras.layers.Flatten(input_shape=(<span class="number">28</span>,<span class="number">28</span>)))  <span class="comment"># 28*28</span></span><br><span class="line">model.add(tf.keras.layers.Dense(<span class="number">32</span>, activation=<span class="string">'relu'</span>))</span><br><span class="line">model.add(tf.keras.layers.Dense(<span class="number">10</span>, activation=<span class="string">'softmax'</span>))</span><br><span class="line"></span><br><span class="line">model.compile(optimizer=tf.keras.optimizers.Adam(learning_rate=<span class="number">0.001</span>),</span><br><span class="line">              loss=<span class="string">'categorical_crossentropy'</span>,</span><br><span class="line">              metrics=[<span class="string">'acc'</span>]</span><br><span class="line">)</span><br><span class="line"></span><br><span class="line">history = model.fit(train_image, train_label_onehot, </span><br><span class="line">                    epochs=<span class="number">10</span>, </span><br><span class="line">                    validation_data=(test_image, test_label_onehot))</span><br></pre></td></tr></table></figure>
<p>作图代码与上面的作图代码一样，这里就不再重复书写了。</p>
<p><img src="https://raw.githubusercontent.com/anxiang1836/FigureBed/master/img/output_47_0.png" style="zoom 85%"></p>
<p>可以看到，这样作出来的模型就是过拟合了！</p>
<h1 id="3-API-tf-keras函数式"><a href="#3-API-tf-keras函数式" class="headerlink" title="3.API-tf.keras函数式"></a>3.API-tf.keras函数式</h1><p>一般顺序模型，就可以按照Sequential()顺序添加就可以了。</p>
<p>那么，如果，所需要构建的模型是分叉的呢？那么我们应该怎么办呢？</p>
<h3 id="Q6：函数式编程"><a href="#Q6：函数式编程" class="headerlink" title="Q6：函数式编程"></a>Q6：函数式编程</h3><p>这里面引入API提供的<strong>函数式编程</strong>。下面给出一个小例子，来展示函数式编程的例子：</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> tensorflow <span class="keyword">as</span> tf</span><br><span class="line"><span class="keyword">from</span> tensorflow <span class="keyword">import</span> keras</span><br><span class="line"></span><br><span class="line">input1 = keras.Input(shape=(<span class="number">28</span>,<span class="number">28</span>))</span><br><span class="line">input2 = keras.Input(shape=(<span class="number">28</span>,<span class="number">28</span>))</span><br><span class="line"><span class="comment"># 与input1和input2分别连接</span></span><br><span class="line">x1 = keras.Flatten()(input1)</span><br><span class="line">x2 = keras.Flatten()(input2)</span><br><span class="line"><span class="comment"># 将x1、x2进行合并</span></span><br><span class="line">x = keras.layers.concatenate([x1,x2])</span><br><span class="line"><span class="comment"># 对x进行全连接</span></span><br><span class="line">x = keras.layers.Dense(<span class="number">32</span>,activation=<span class="string">'relu'</span>)(x)</span><br><span class="line"><span class="comment"># 对x进行逻辑回归输出</span></span><br><span class="line">out = keras.layers.Dense(<span class="number">1</span>,activation=<span class="string">'sigmoid'</span>)(x)</span><br><span class="line"></span><br><span class="line"><span class="comment"># 最终定义模型的输入和输出</span></span><br><span class="line">model = keras.Model(inputs=[input1,input2],outputs=out)</span><br></pre></td></tr></table></figure>
<p>那么，我们现在看一下模型最终的形态：</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line">Model: "model_1"</span><br><span class="line">__________________________________________________________________________________________</span><br><span class="line">Layer (type)                    Output Shape         Param <span class="comment">#     Connected to                     </span></span><br><span class="line">==========================================================================================</span><br><span class="line">input_2 (InputLayer)            [(None, 28, 28)]     0                                            </span><br><span class="line">__________________________________________________________________________________________</span><br><span class="line">input_3 (InputLayer)            [(None, 28, 28)]     0                                            </span><br><span class="line">__________________________________________________________________________________________</span><br><span class="line">flatten_1 (Flatten)             (None, 784)          0           input_2[0][0]                    </span><br><span class="line">__________________________________________________________________________________________</span><br><span class="line">flatten_2 (Flatten)             (None, 784)          0           input_3[0][0]                    </span><br><span class="line">__________________________________________________________________________________________</span><br><span class="line">concatenate (Concatenate)       (None, 1568)         0           flatten_1[0][0]                  </span><br><span class="line">                                                                 flatten_2[0][0]                  </span><br><span class="line">__________________________________________________________________________________________</span><br><span class="line">dense_3 (Dense)                 (None, 32)           50208       concatenate[0][0]                </span><br><span class="line">__________________________________________________________________________________________</span><br><span class="line">dense_4 (Dense)                 (None, 1)            33          dense_3[0][0]                    </span><br><span class="line">==========================================================================================</span><br><span class="line">Total params: 50,241</span><br><span class="line">Trainable params: 50,241</span><br><span class="line">Non-trainable params: 0</span><br><span class="line">__________________________________________________________________________________________</span><br></pre></td></tr></table></figure>

      
    </div>
    <footer class="article-footer">
      
      
      
        
	<div id="comment">
		<!-- 来必力City版安装代码 -->
		<div id="lv-container" data-id="city" data-uid="MTAyMC80NTk2OS8yMjQ4MA==">
		<script type="text/javascript">
		   (function(d, s) {
		       var j, e = d.getElementsByTagName(s)[0];

		       if (typeof LivereTower === 'function') { return; }

		       j = d.createElement(s);
		       j.src = 'https://cdn-city.livere.com/js/embed.dist.js';
		       j.async = true;

		       e.parentNode.insertBefore(j, e);
		   })(document, 'script');
		</script>
		<noscript>为正常使用来必力评论功能请激活JavaScript</noscript>
		</div>
		<!-- City版安装代码已完成 -->
	</div>



      
      
        
  <ul class="article-tag-list"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/tf-keras/">tf.keras</a></li></ul>

      

    </footer>
  </div>
  
    
<nav id="article-nav">
  
    <a href="/2019/10/05/NLP_language_recognizier_Base_NB/" id="article-nav-newer" class="article-nav-link-wrap">
      <strong class="article-nav-caption">上一篇</strong>
      <div class="article-nav-title">
        
          【NLP】用朴素贝叶斯完成语种识别器
        
      </div>
    </a>
  
  
    <a href="/2019/08/27/Tools_Mac_zsh/" id="article-nav-older" class="article-nav-link-wrap">
      <strong class="article-nav-caption">下一篇</strong>
      <div class="article-nav-title">【工具篇】Mac终端美化：iterm2 + zsh + oh~my~zsh 设置教程</div>
    </a>
  
</nav>

  
</article>

<!-- Table of Contents -->

  <aside id="toc-sidebar">
    <div id="toc" class="toc-article">
    <strong class="toc-title">文章目录</strong>
    
        <ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#1-API—-tf-keras基础"><span class="nav-number">1.</span> <span class="nav-text">1.API—-tf.keras基础</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#2-fashion-mnist分类练习"><span class="nav-number">2.</span> <span class="nav-text">2.fashion_mnist分类练习</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#2-1数据读取"><span class="nav-number">2.1.</span> <span class="nav-text">2.1数据读取</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-2-数据归一化"><span class="nav-number">2.2.</span> <span class="nav-text">2.2 数据归一化</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-3-按label类型分情况训练"><span class="nav-number">2.3.</span> <span class="nav-text">2.3 按label类型分情况训练</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#2-3-1-label为序列编码"><span class="nav-number">2.3.1.</span> <span class="nav-text">2.3.1 label为序列编码</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-3-2-label为One-Hot编码"><span class="nav-number">2.3.2.</span> <span class="nav-text">2.3.2 label为One-Hot编码</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Q1：evaluate和predict区别"><span class="nav-number">2.3.3.</span> <span class="nav-text">Q1：evaluate和predict区别?</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Q2：网络容量"><span class="nav-number">2.3.4.</span> <span class="nav-text">Q2：网络容量</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Q3：提高网络的拟合能力"><span class="nav-number">2.3.5.</span> <span class="nav-text">Q3：提高网络的拟合能力</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Q4：超参数选择的原则"><span class="nav-number">2.3.6.</span> <span class="nav-text">Q4：超参数选择的原则</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-4-Dropout防止过拟合"><span class="nav-number">2.4.</span> <span class="nav-text">2.4 Dropout防止过拟合</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#fit中添加validation-data"><span class="nav-number">2.4.1.</span> <span class="nav-text">fit中添加validation_data</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Q5：过拟合-amp-amp-欠拟合"><span class="nav-number">2.4.2.</span> <span class="nav-text">Q5：过拟合&amp;&amp;欠拟合</span></a></li></ol></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#3-API-tf-keras函数式"><span class="nav-number">3.</span> <span class="nav-text">3.API-tf.keras函数式</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#Q6：函数式编程"><span class="nav-number">3.0.1.</span> <span class="nav-text">Q6：函数式编程</span></a></li></ol></li></ol></li></ol>
    
    </div>
  </aside>

</section>
        
      </div>
      
      <footer id="footer">
  

  <div class="container">
      	<div class="row">
	      <p> Powered by <a href="http://hexo.io/" target="_blank">Hexo</a> and <a href="https://github.com/iTimeTraveler/hexo-theme-hiker" target="_blank">Hexo-theme-hiker</a> </p>
	      <p id="copyRightEn">Copyright &copy; 2013 - 2020 MaxMa All Rights Reserved.</p>
	      
	      
    		<p class="busuanzi_uv">
				访客数 : <span id="busuanzi_value_site_uv"></span> |  
				访问量 : <span id="busuanzi_value_site_pv"></span>
		    </p>
  		   
		</div>

		
  </div>
</footer>


<!-- min height -->

<script>
    var wrapdiv = document.getElementById("wrap");
    var contentdiv = document.getElementById("content");
    var allheader = document.getElementById("allheader");

    wrapdiv.style.minHeight = document.body.offsetHeight + "px";
    if (allheader != null) {
      contentdiv.style.minHeight = document.body.offsetHeight - allheader.offsetHeight - document.getElementById("footer").offsetHeight + "px";
    } else {
      contentdiv.style.minHeight = document.body.offsetHeight - document.getElementById("footer").offsetHeight + "px";
    }
</script>
    </div>
    <!-- <nav id="mobile-nav">
  
    <a href="/" class="mobile-nav-link">Home</a>
  
    <a href="/archives" class="mobile-nav-link">Archives</a>
  
    <a href="/categories" class="mobile-nav-link">Categories</a>
  
    <a href="/tags" class="mobile-nav-link">Tags</a>
  
    <a href="/about" class="mobile-nav-link">About</a>
  
</nav> -->
    

<!-- mathjax config similar to math.stackexchange -->

<script type="text/x-mathjax-config">
  MathJax.Hub.Config({
    tex2jax: {
      inlineMath: [ ['$','$'], ["\\(","\\)"] ],
      processEscapes: true
    }
  });
</script>

<script type="text/x-mathjax-config">
    MathJax.Hub.Config({
      tex2jax: {
        skipTags: ['script', 'noscript', 'style', 'textarea', 'pre', 'code']
      }
    });
</script>

<script type="text/x-mathjax-config">
    MathJax.Hub.Queue(function() {
        var all = MathJax.Hub.getAllJax(), i;
        for(i=0; i < all.length; i += 1) {
            all[i].SourceElement().parentNode.className += ' has-jax';
        }
    });
</script>

<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.1/MathJax.js?config=TeX-AMS-MML_HTMLorMML">
</script>


  <link rel="stylesheet" href="/fancybox/jquery.fancybox.css">
  <script src="/fancybox/jquery.fancybox.pack.js"></script>


<script src="/js/scripts.js"></script>




  <script src="/js/dialog.js"></script>








	<div style="display: none;">
    <script src="https://s95.cnzz.com/z_stat.php?id=1260716016&web_id=1260716016" language="JavaScript"></script>
  </div>



	<script async src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js">
	</script>






  </div>

  <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true" style="display: none;">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header">
        <h2 class="modal-title" id="myModalLabel">设置</h2>
      </div>
      <hr style="margin-top:0px; margin-bottom:0px; width:80%; border-top: 3px solid #000;">
      <hr style="margin-top:2px; margin-bottom:0px; width:80%; border-top: 1px solid #000;">


      <div class="modal-body">
          <div style="margin:6px;">
            <a data-toggle="collapse" data-parent="#accordion" href="#collapseOne" onclick="javascript:setFontSize();" aria-expanded="true" aria-controls="collapseOne">
              正文字号大小
            </a>
          </div>
          <div id="collapseOne" class="panel-collapse collapse" role="tabpanel" aria-labelledby="headingOne">
          <div class="panel-body">
            您已调整页面字体大小
          </div>
        </div>
      


          <div style="margin:6px;">
            <a data-toggle="collapse" data-parent="#accordion" href="#collapseTwo" onclick="javascript:setBackground();" aria-expanded="true" aria-controls="collapseTwo">
              夜间护眼模式
            </a>
        </div>
          <div id="collapseTwo" class="panel-collapse collapse" role="tabpanel" aria-labelledby="headingTwo">
          <div class="panel-body">
            夜间模式已经开启，再次单击按钮即可关闭 
          </div>
        </div>

        <div>
            <a data-toggle="collapse" data-parent="#accordion" href="#collapseThree" aria-expanded="true" aria-controls="collapseThree">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;关 于&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</a>
        </div>
         <div id="collapseThree" class="panel-collapse collapse" role="tabpanel" aria-labelledby="headingThree">
          <div class="panel-body">
            MaxMa
          </div>
          <div class="panel-body">
            Copyright © 2020 MaxMa All Rights Reserved.
          </div>
        </div>
      </div>


      <hr style="margin-top:0px; margin-bottom:0px; width:80%; border-top: 1px solid #000;">
      <hr style="margin-top:2px; margin-bottom:0px; width:80%; border-top: 3px solid #000;">
      <div class="modal-footer">
        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
      </div>
    </div>
  </div>
</div>
  
  <a id="rocket" href="#top" class=""></a>
  <script type="text/javascript" src="/js/totop.js?v=1.0.0" async=""></script>
  
    <a id="menu-switch"><i class="fa fa-bars fa-lg"></i></a>
  
<script type="text/x-mathjax-config">
    MathJax.Hub.Config({
        tex2jax: {
            inlineMath: [ ["$","$"], ["\\(","\\)"] ],
            skipTags: ['script', 'noscript', 'style', 'textarea', 'pre', 'code'],
            processEscapes: true
        }
    });
    MathJax.Hub.Queue(function() {
        var all = MathJax.Hub.getAllJax();
        for (var i = 0; i < all.length; ++i)
            all[i].SourceElement().parentNode.className += ' has-jax';
    });
</script>
<script src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>
</body>
</html>